// pages/personal/personal.js
const util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*  */
    showNoDataIcon:false,
    userInfo_sj: {},
    /*  */
    storeName:'',
    address:'',
    mobile:'',
    level:'',
    levelArr:[],
    levelArr0:[], //灰色
    /*  */
    adviceBtn:[
      {}
    ],
    /*  */
    bindPhone:[
      {}
    ],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /* 获取登录用户的后台信息 */
  user_loginWx: function () {
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.Url + '/loginWxStore',
        method: 'POST',
        header: {
          'content-type': app.globalData.contentType
        },
        data: util.json2Form({
          avatar: this.data.userInfo_sj.userInfo.avatar,  //头像
          nickName: this.data.userInfo_sj.userInfo.nickName,  //昵称
          openid: this.data.userInfo_sj.openid,    //openid
        }),
        success: res => {
          console.log(res)
          this.setData({
            'userInfo_sj.token': res.data.body.token,
            'userInfo_sj.header.cartoken': res.data.body.token,
            'userInfo_sj.userid': res.data.body.userid,
            'userInfo_sj.mobile': res.data.body.mobile,
            'userInfo_sj.storeId': res.data.body.storeId||'',
            'userInfo_sj.storeName': res.data.body.storeName||'',
            'userInfo_sj.address': res.data.body.address||'暂未添加门店地址',
            'userInfo_sj.level': res.data.body.level||0,
          })
          //信息存缓存
          wx.setStorageSync('userInfo_sj', this.data.userInfo_sj)
          resolve()
        },
        fail: err => {
          reject('获取与登录用户对应的后台信息失败')
        }
      })
    })
  },
  /* 获取用户信息 */
  getUserInfoFn: function (e) {
    console.log(e)
    if (e.detail.errMsg == 'getUserInfo:ok') {
      wx.showLoading({
        title: '跳转中...'
      })
      this.setData({
        'userInfo_sj.userInfo': e.detail.userInfo
      })
      this.user_loginWx().then(() => {
        /*  */
        if(!this.data.userInfo_sj.mobile){
          wx.navigateTo({
            url: '/pages/login2/login2'
          })
        }else{
          wx.switchTab({
            url: '/pages/index/index'
          })
        }
      }).catch(errMsg => {
        wx.showModal({
          title: '提示',
          content: errMsg + '',
          showCancel: false,
          confirmColor: app.globalData.modalConfirmColor
        })
      }).finally(() => {
        wx.hideLoading()
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请允许授权',
        showCancel: false,
        confirmColor: app.globalData.modalConfirmColor
      })
    }
  },
  /* 退出登录 */
  exitLogin: function(){
    wx.showModal({
      title:'提示',
      content:'确定退出登录吗？',
      success: res=>{
        if(res.confirm){
          wx.showLoading({
            title: '退出中...',
            mask:true
          })
          // 清空手机号码
          wx.request({
            url: app.globalData.Url + '/wx/clearUserMobile',
            method: 'POST',
            header: this.data.userInfo_sj.header,
            data: util.json2Form({
              type: this.data.userInfo_sj.miniProType
            }),
            success: res => {
              console.log(res)
              //判断token是否过期
              if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
              if (res.data.code == '1') {
                wx.clearStorageSync('userInfo_sj')
                //
                wx.navigateTo({
                  url:'/pages/login/login'
                })
              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.data.msg + ''
                })
              }
            },
            fail: err => {
              console.log(err)
              wx.showToast({
                icon: 'none',
                title: '清空手机号码失败'
              })
              reject()
            },
            complete: () => {
              wx.hideLoading()
            }
          })
        }
      }
    })
  },
  /*  */
  showAdvicePage: function(e){
    let { index } = e.currentTarget.dataset
    app.globalData.setNeedAnimation.call(this, 'adviceBtn', this.data.adviceBtn, index).then(()=>{
      wx.navigateTo({
        url: '/pages/advice/advice',
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户信息
    this.setData({
      userInfo_sj: app.globalData.get_userInfo()
    })
    console.log(this.data.userInfo_sj)
    if(this.data.userInfo_sj.mobile){
      let tmpLevel = this.data.userInfo_sj.level*1
      if(isNaN(tmpLevel)){
        tmpLevel = 0
      }
      if(tmpLevel>5){
        tmpLevel = 5
      }
      let tmpArr = []
      for(var i = 0; i < tmpLevel; i++){
        tmpArr.push(i)
      }
      let tmpArr0 = []
      if(tmpLevel<5){
        let abs = 5 - tmpLevel
        for(var i = 0; i < abs; i++){
          tmpArr0.push(i)
        }
      }
      //
      this.setData({
        levelArr: tmpArr,
        levelArr0: tmpArr0
      })
    }
    //console.log(this.data.levelArr)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})